<template>
	<view class="p-30 fl-s-s">
		<view class="fr-s-c">
			<view class="mainbar"></view>
			<text class="c-00 fs-28 m-l-10 bold">人工费</text>
		</view>
		<text class="c-77 m-t-10 fs-24">请选择或输入所需工时</text>
		<view class="w-1 fr-b-s warp">
			<view class="fr-c m-t-20" @click="(cur=index)" :class="cur==index?'houract':'hour'"
				v-for="(item,index) in 8" :key="index">{{item}}小时</view>
		</view>
		<view class="ibox fr-s-c p-r-30 m-t-20">
			<input type="number" v-model="artduratime" placeholder="请输入所需工时" class="flex-1 fs-24" />
			<text class="c-33 fs-24">小时</text>
		</view>
		<view class="ibox fr-s-c p-r-30 m-t-20">
			<input type="digit" v-model="numPrice" placeholder="请输入每小时单价" class="flex-1 fs-24" />
			<text class="c-33 fs-24">元/小时</text>
		</view>
		<view class="fr-s-c m-t-40">
			<view class="mainbar"></view>
			<text class="c-00 fs-28 m-l-10 bold">材料费</text>
		</view>

		<view class="fr-s-c m-t-30">
			<text class="c-00 fs-28 bold">￥</text>
			<input type="digit" v-model="materprice" class="flex-1 fs-28 m-l-20" placeholder="请输入材料金额" />
		</view>
		<view class="w-1 bb-solid m-t-20"></view>
		<text class="c-33 fs-28 bold m-t-40">请上传照片</text>
		<view class="w-1 fl-s-s m-t-20">
			<up-upload :sizeType="['compressed']" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"
				name="1" :multiple="true" width="156rpx" height="156rpx" :maxCount="6">
				<view class="fl-c br-20" style="width: 156rpx; height: 156rpx; background: #f3f3f3;">
					<image :src="$path + '/tab1/camera.png'" style="width: 51rpx; height: 49rpx;" mode=""></image>
					<text class="fs-20 c-77">上传图片</text>
				</view>
			</up-upload>
		</view>

		<view class="iphonex">
			<view class="w-1 p-30">
				<view class="big-btn" @click="sub">确定</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		baseUrl
	} from '@/utils/base.js';
	import {
		watch
	} from 'vue';
	const id = ref(0)
	onLoad(o => {
		id.value = o.id;
	})

	const cur = ref(0)

	const artduratime = ref('')
	const materprice = ref('')
	watch(artduratime, val => {
		cur.value = val - 1
	})

	const inventory = ref('')
	const numPrice = ref(100)
	const fileList1 = ref([]);
	watch(fileList1, val => {
		console.log('图片val', val)
		let arr = []
		val.map(item => {
			if (item.status == 'success') {
				// arr.push('/uploads/' + item.url.split('/uploads/')[1])
				arr.push(item.url)
			}
		})
		inventory.value = arr.join(',')
	}, {
		deep: true
	})


	function sub() {
		if (cur.value == -1 && !artduratime.value.trim().length) return uni.$u.toast('请选择或输入工时');
		if (!materprice.value.trim().length) return uni.$u.toast('请输入材料费用');
		if (!inventory.value) return uni.$u.toast('请上传图片');
		uni.navigateTo({
			url: './Quotation2?info=' + encodeURIComponent(JSON.stringify({
				artduratime: artduratime.value || cur.value * 1 + 1,
				materprice: materprice.value,
				inventory: inventory.value,
				aid: id.value,
				type: 6,
				numPrice: numPrice.value
			}))
		})
	}

	// 删除图片
	const deletePic = (event) => {
		const arr = ref([]);
		switch (event.name * 1) {
			case 1:
				arr.value = fileList1.value;
				break;
			case 2:
				arr.value = fileList2.value;
				break;
			case 3:
				arr.value = fileList3.value;
				break;
		}
		arr.value.splice(event.index, 1);
	};
	// 新增图片
	const afterRead = async (event) => {
		const arr = ref([]);
		switch (event.name * 1) {
			case 1:
				arr.value = fileList1.value;
				break;
			case 2:
				arr.value = fileList2.value;
				break;
			case 3:
				arr.value = fileList3.value;
				break;
		}
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = arr.value.length;
		lists.map((item) => {
			arr.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url);
			let item = arr.value[fileListLen];
			arr.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
	};

	const uploadFilePromise = (url) => {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: baseUrl + '/api/Common/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				header: {
					token: uni.getStorageSync('user').token
				},
				success: (res) => {
					setTimeout(() => {
						resolve(JSON.parse(res.data).data.fullurl)
					}, 1000);
				},
			});
		});
	};
</script>

<style>
	.mainbar {
		width: 4rpx;
		height: 20rpx;
		background: #01796D;
		border-radius: 2rpx 2rpx 2rpx 2rpx;
	}

	.hour {
		width: 156rpx;
		height: 66rpx;
		background: #FFFFFF;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		border: 2rpx solid #E9E9E9;
		font-size: 24rpx;
		color: #3E3E3E;
	}

	.houract {
		width: 156rpx;
		height: 66rpx;
		background: #01796D;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		font-size: 24rpx;
		color: #FFFFFF;
	}

	.ibox {
		width: 686rpx;
		height: 72rpx;
		background: #FFFFFF;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		border: 2rpx solid #E9E9E9;
	}
</style>